package com.ce.controller;


import com.ce.config.FileUtil;
import com.ce.entity.Customer;
import com.ce.entity.Product;
import com.ce.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ce
 * @since 2022-01-26
 */
@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {
    @Autowired
    ProductService service;
    @Autowired
    FileUtil fileUtil;
    /**
     * 查询商品信息
     */
    @PostMapping("/getProductist")
    public List<Product> testSelect(Product product){
        List<Product> products = service.getList(product);
        return products;

    }
    /**
     * 删除商品
     */
    @DeleteMapping("/deleteProduct")
    public Integer deleteProduct(Integer id){
        Integer res =service.deleteById(id);
        return res;
    }

    /**
     * 获取localDate
     */
    private LocalDate getLocalDate(java.util.Date date) {
        //将参数转为系统默认的格式
        java.util.Date date1 = new Date(date.getTime());
        Instant instant = date1.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate;
    }

    /**
     * 上传图片文件
     *
     * @param multipartFile
     * @return url
     */
    @PostMapping("upload")
    public String upload(@RequestParam("file")  MultipartFile multipartFile) {
        return fileUtil.upload(multipartFile, "test1");
    }

    /**
     * 添加商品
     */
    @PostMapping("/addProduct")
    public Map<String, String> insertProduct(HttpServletRequest request) throws ParseException {
        Product product = new Product();
        product.setProductNo(request.getParameter("productNo"));
        product.setProductName(request.getParameter("productName"));
        product.setProductType(request.getParameter("productType"));
        /*product.setProductNumber(Integer.parseInt(request.getParameter("productNumber")));*/
        System.out.println("imgurl:"+request.getParameter("imgUrl"));
        product.setImgUrl(request.getParameter("imgUrl"));
        product.setProductPrice(Double.parseDouble(request.getParameter("productPrice")));
        product.setProductOrign(request.getParameter("productOrign"));
        product.setProductBirthdate(request.getParameter("productBirthdate"));
        product.setProductValiddate(request.getParameter("productValiddate"));
        product.setProductProvidername(request.getParameter("productProvidername"));
        return service.addProduct(product);
    }


    /**
     * 修改商品信息
     */
    @PostMapping("/updateProduct")
    public Map<String, String> updateProduct(HttpServletRequest request) throws ParseException {
        Product product = new Product();
        product.setId(Integer.parseInt(request.getParameter("id")));
        product.setProductNo(request.getParameter("productNo"));
        product.setProductName(request.getParameter("productName"));
        product.setProductType(request.getParameter("productType"));
        product.setImgUrl(request.getParameter("imgUrl"));
        /*product.setProductNumber(Integer.parseInt(request.getParameter("productNumber")));*/
        product.setProductPrice(Double.parseDouble(request.getParameter("productPrice")));
        product.setProductOrign(request.getParameter("productOrign"));
        product.setProductBirthdate(request.getParameter("productBirthdate"));
        product.setProductValiddate(request.getParameter("productValiddate"));
        product.setProductProvidername(request.getParameter("productProvidername"));
        return service.updateProduct(product);
    }


}
